<?php
namespace app\admin\validate;

use app\admin\model\Config;
use think\exception\ValidateException;

class ConfigValidate extends AdminValidate
{
	protected $rule = [
		'group' => 'require',
		'type' => 'require',
		'title' => 'require',
		'key' => 'require|alphaDash|checkKey',
        'value' => 'require'
	];

	protected $message = [
		'group.require' => '请选择一个配置分组',
		'type.require' => '请选择配置类型',
		'title.require' => '请输入配置名称',
		'key.require' => '请输入配置标识',
		'key.alphaDash' => '配置标识只能是数字、字母、_、-',
        'value.require' => '请输入配置内容'
	];
	
	protected $scene = [
        'update' => ['group','type','title','value']
    ];

	protected function checkKey($value,$rule,$data=[]){
		$group = $data['group'];
//		throw new ValidateException($this->currentScene);
		if(is_numeric($value[0]) || $value[0] == '-'){
			throw new ValidateException('配置标识的第一个字符只能是字母、_');
		}

        if($this->currentScene != 'update'){
            $config = Config::where(['group'=>$group,'key'=>$value])->findOrEmpty();
            if(!$config->isEmpty()){
                throw new ValidateException('配置标识已存在');
            }
        }

		return true;
	}

}

